<?php

namespace app\index\controller;

use think\Db;

class JobDuty extends Base
{

    /**
     * 添加职责
     */
    public function addDuty()
    {
        $data = [];
        input('name') ? $data['name'] = input('name') : $this->err('缺少职责');
        $data['createTime'] = createTime();

        $res = model('job_duty')->editData($data);
        $res ? $this->suc($res) : $this->err('添加职责失败');
    }

    /**
     * 修改职责
     */
    public function editDuty()
    {
        $data = [];
        input('id') ? $data['id'] = input('id') : $this->err('缺少职责主键');
        input('name') ? $data['name'] = input('name') : $this->err('缺少职责');

        $res = model('job_duty')->editData($data, $data['id']);
        $res ? $this->suc($res) : $this->err('修改职责失败');
    }

    /**
     * 删除职责
     */
    public function delDuty()
    {
        $where = [];
        input('id') ? $where['id'] = input('id') : $this->err('缺少职责主键');

        $res = model('job_duty')->where($where)->delete();
        $res ? $this->suc($res) : $this->err('删除职责失败');
    }


    /**
     * 根据岗位查询职责
     */
    public function getDutyListByJobId()
    {
        $where = [];
        input('jobId') ? $where['j.id'] = input('jobId') : $this->err('缺少岗位编码！');
        $res = model('job_duty')
            ->alias('jd')
            ->field('jd.id,jd.name as dutyName,j.name as jobName,j.jobSalary,j.maxSalary,j.minSalary')
            ->join('job j', 'j.id=jd.jobId', 'left')
            ->where($where)
            ->order('jd.id desc')
            ->paginate(10)
            ->toArray();
        $res ? $this->suc($res) : $this->err('获取失败');
    }

}
